package com.syq.face.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.syq.face.MyApplication;
import com.syq.face.R;

import java.util.ArrayList;
import java.util.List;

import me.yokeyword.fragmentation_swipeback.SwipeBackFragment;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link RootFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class RootFragment extends SwipeBackFragment {


    public static final String TAG = "MainActivity";
    private TabLayout tabLayout;
    private ViewPager2 viewpager;
    private FragmentManager fragmentManager;


    //持有所有的展示的Fragment
    private List<NotifiedFragment> fragments = new ArrayList<>();

    private static final int[] tabIcons = {R.drawable.kaoqin,R.drawable.me
    };
    private static final String[] tabTexts = {"所属组","我的"};

    public RootFragment() {

    }


    public static RootFragment newInstance() {
        RootFragment fragment = new RootFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_root, container, false);
        fragmentManager = getChildFragmentManager();
        init(rootView);

        return rootView;
    }
    private void initView(View rootView) {
        tabLayout = rootView.findViewById(R.id.fragment_root_tl);
        viewpager = rootView.findViewById(R.id.fragment_root_vp2);
    }

    private void init(View rootView) {
        initView(rootView);
        fragments.add(new GroupFragment());
        fragments.add(new MyFragment());
        initViewPager();
    }

    private void initViewPager() {
        viewpager.setAdapter(new FragmentStateAdapter(fragmentManager, getLifecycle()) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return fragments.get(position);
            }

            @Override
            public int getItemCount() {
                return fragments.size();
            }
        });
        //将tabLayout与viewpager进行绑定
        new TabLayoutMediator(tabLayout, viewpager, false, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                Log.d(TAG, "onConfigureTab: ");
                tab.setText(tabTexts[position]);
                tab.setIcon(tabIcons[position]);
                fragments.get(position).notifyDataReload();

            }
        }).attach();
        viewpager.setCurrentItem(0);

    }
}